;; ID stuff
(setq user-mail-address "emilia.blasten@iki.fi"
      mail-host-address "iki.fi"
      user-full-name    "Emilia Blåsten"
      message-dont-reply-to-names '("emilia.blasten@iki.fi"
				    "emilia.blasten@lut.fi"
				    "emilia@tranfem.fi"
				    "emilia@transfeminiinit.fi"))

;; Password xoauth2 stuff
(require 'auth-source-xoauth2)
(require 'gnus-secrets "~/.gnus-secrets.el.gpg")
(setq auth-source-xoauth2-creds 'my-xoauth2-get-secrets)
(add-to-list 'smtpmail-auth-supported 'xoauth2)
(auth-source-xoauth2-enable)

;; IMAP stuff
(setq gnus-select-method '(nnnil nil))

(setq gnus-secondary-select-methods '((nnimap "cmail"
                                              (nnimap-address "imap1.countermail.com")
                                              (nnimap-server-port 993)
                                              (nnimap-stream tls))
                                      (nnimap "transfem"
                                              (nnimap-address "imap.gmail.com")
					      (nnimap-user "emilia@transfeminiinit.fi")
					      (nnimap-authenticator xoauth2)
					      (nnimap-server-port 993)
                                              (nnimap-stream tls))
				      (nnimap "LUT"
					      (nnimap-address "outlook.office365.com")
					      (nnimap-user "emilia.blasten@lut.fi")
					      (nnimap-authenticator xoauth2)
                                              (nnimap-server-port 993)
                                              (nnimap-stream tls))
				      (nnimap "mailbox.org"
                                              (nnimap-address "imap.mailbox.org")
                                              (nnimap-server-port 993)
                                              (nnimap-stream tls))
				      (nnimap "youtuber"
                                              (nnimap-address "imap.gmail.com")
					      (nnimap-user "cursed.emilia@gmail.com")
					      (nnimap-authenticator xoauth2)
                                              (nnimap-server-port 993)
                                              (nnimap-stream tls))))


;; Level stuff (for speed etc.)
; Read
; https://www.gnu.org/software/emacs/manual/html_node/gnus/Group-Levels.html
; and
; https://emacs.stackexchange.com/questions/34537/how-to-prevent-gnus-from-querying-the-server-during-startup
(setq gnus-activate-level 5) ; Default 6, groups with level above this
			     ; won't be activated. Use 'S l' on a
			     ; group to give it a new level (if you
			     ; want to make it activated or not). Use
			     ; e.g. '6 g' to refresh all groups level
			     ; 6 or below.
(setq gnus-check-new-newsgroups nil) ; don't check server for new
				     ; stuff. Use 'U' in group buffer
				     ; to addd new groups.
;; smallest set of conditions first
(with-eval-after-load 'gnus-group
  (add-to-list 'gnus-group-highlight
	       '((and mailp (>= level 4) (<= level 6))
		 . gnus-group-mail-3))
  (add-to-list 'gnus-group-highlight
	       '((and mailp (= unread 0) (>= level 4) (<= level 6))
		 . gnus-group-mail-3-empty)))


;; SMTP stuff & sending related stuff
(setq smtpmail-smtp-server "smtp.iki.fi"
      smtpmail-stream-type 'starttls
      smtpmail-smtp-service 587
      gnus-gcc-mark-as-read t)
(with-eval-after-load 'smtpmail
  (add-to-list 'smtpmail-auth-supported 'xoauth2)) ; add "smtp-auth XXX" to .authinfo.gpg to select xoauth2/cram-md5/login/plain as an authentication type if the default fails.
(setq gnus-message-archive-group
      '(("LUT" "nnimap+LUT:INBOX") ; Items seem to go "nnimap+LUT:Sent Items" automatically. The server does that. Just clear that group every now and then...
	("transfem" "nnimap+transfem:INBOX")
	("cmail" "nnimap+cmail:INBOX")
	("youtuber" "nnimap+youtuber:INBOX")))
(setq gnus-posting-styles
      '((".*" ; Matches all groups of messages
         (address "Emilia Blåsten <emilia.blasten@iki.fi>")
	 ("X-Message-SMTP-Method" "smtp smtp.iki.fi 587 emilia.blasten@iki.fi"))
        ;; ("mailbox.org" ; Matches Gnus group called "mailbox.org"
        ;;  (address "Emilia Blåsten <drblasten@mailbox.org>")
        ;;  ("X-Message-SMTP-Method" "smtp smtp.mailbox.org 587 drblasten@mailbox.org"))
	("transfem" ; Matches Gnus group called "transfem"
         (address "Emilia Blåsten <emilia@transfeminiinit.fi>")
         (organization "Transfeminiinit ry")
         ("X-Message-SMTP-Method" "smtp smtp.gmail.com 587 emilia@transfeminiinit.fi"))
	("LUT" ; Matches Gnus group called "LUT"
         (address "Emilia L.K. Blåsten <emilia.blasten@lut.fi>")
         (organization "LUT University")
         ("X-Message-SMTP-Method" "smtp smtp-mail.outlook.com 587 emilia.blasten@lut.fi"))
	("youtuber" ; Matches Gnus group called "youtuber"
         (address "Cursed Emilia <cursed.emilia@gmail.com>")
         ("X-Message-SMTP-Method" "smtp smtp.gmail.com 587 cursed.emilia@gmail.com"))))


;; Interface stuff
(setq gnus-summary-line-format "%U%R %&user-date; %B%-31,31s %4k %uB\n"
      ;; Default with %f ('Name, To header or Newsgroups header')
      ;; replaced by %uB (BBDB Name + Known poster indicator '+')
      gnus-user-date-format-alist '((t . "%Y-%m-%d %H:%M"))
      gnus-summary-thread-gathering-function 'gnus-gather-threads-by-references
      gnus-summary-gather-subject-limit 'fuzzy
      gnus-fetch-old-headers t
      gnus-article-sort-functions '(gnus-article-sort-by-date)
      gnus-thread-sort-functions '((not gnus-thread-sort-by-most-recent-date))
      gnus-subthread-sort-functions '(gnus-thread-sort-by-date)
      gnus-sum-thread-tree-false-root ""
      gnus-sum-thread-tree-indent " "
      gnus-sum-thread-tree-leaf-with-other "├► "
      gnus-sum-thread-tree-root ""
      gnus-sum-thread-tree-single-leaf "╰► "
      gnus-sum-thread-tree-vertical "│")

;; Set group parameters. Press "G c" on a group name and -more- to find more info.
(setq gnus-parameters
      '(("cmail:INBOX" ; Emilia Countermail
	 (auto-expire . nil)
	 (expiry-wait . 7)
	 (expiry-target . "nnimap+cmail:Trash"))
	("cmail:Spam"
	 (total-expire . t)
	 (expiry-wait . 7)
	 (expiry-target . "nnimap+cmail:Trash"))
	("cmail:Trash"
	 (auto-expire . t)
	 (expiry-wait . 31)
	 (expiry-target . delete))
	("transfem:INBOX" ; Emilia Transfeminiinit hallitus
	 (auto-expire . nil)
	 (expiry-wait . 7)
	 (expiry-target . "nnimap+transfem:[Gmail]/Bin"))
	("transfem:[Gmail]/Spam"
	 (total-expire . t)
	 (expiry-wait . 7)
	 (expiry-target . "nnimap+tranfem:[Gmail]/Bin"))
	("transfem:[Gmail]/Bin"
	 (auto-expire . t)
	 (expiry-wait . 31)
	 (expiry-target . delete))
	("LUT:INBOX" ; LUT University
	 (auto-expire . nil)
	 (expiry-wait . 7)
	 (expiry-target . "nnimap+LUT:Deleted Items"))
	("LUT:Junk Email"
	 (total-expire . t)
	 (expiry-wait . 7)
	 (expiry-target . "nnimap+LUT:Deleted Items"))
	("LUT:Deleted Items"
	 (auto-expire . t)
	 (expiry-wait . 31)
	 (expiry-target . delete))
	("LUT:Trash"
	 (auto-expire . t)
	 (expiry-wait . 31)
	 (expiry-target . delete))
	("mailbox.org:INBOX" ; Emilia mailbox.org
	 (total-expire . t) ;see below suppress duplicates
	 (expiry-wait . 7)
	 (expiry-target . "nnimap+mailbox.org:Trash"))
	("mailbox.org:Junk"
	 (total-expire . t)
	 (expiry-wait . 7)
	 (expiry-target . "nnimap+mailbox.org:Trash"))
	("mailbox.org:Trash"
	 (total-expire . t) ; Because duplicate suppression doesn't put the E symbol on them. Marks them read though.
	 (expiry-wait . 31)
	 (expiry-target . delete))
	("youtuber:INBOX" ; Cursed Emilia gmail
	 (auto-expire . nil)
	 (expiry-wait . 7)
	 (expiry-target . "nnimap+youtuber:[Gmail]/Bin"))
	("youtuber:[Gmail]/Spam"
	 (total-expire . t)
	 (expiry-wait . 7)
	 (expiry-target . "nnimap+youtuber:[Gmail]/Bin"))
	("youtuber:[Gmail]/Bin"
	 (auto-expire . t)
	 (expiry-wait . 31)
	 (expiry-target . delete))))
(setq gnus-suppress-duplicates t) ;make it so a read article marks duplicated read when seen them. So mailbox.org articles are marked as read (and total-expire moves them to Trash) when reading them elsewhere. WARNING: reading in mailbox.org marks them read elsewhere too.

;; Security stuff
(setq mm-verify-option "always"
      gnus-buttonized-mime-types '("multipart/signed") ; show verified key etc
;      mm-sign-option 'guided ; for always selecting signing key from a menu
      mml-secure-openpgp-encrypt-to-self t
      mml-secure-smime-encrypt-to-self t
      mml-secure-openpgp-sign-with-sender t
      mml-secure-smime-sign-with-sender t
      gnus-message-replysignencrypted t)
(add-hook 'gnus-message-setup-hook 'mml-secure-message-sign-pgpmime) ; Always sign
